<template>
	<view>
		<u--form labelPosition="left">
			<u-form-item label="收货人" borderBottom labelWidth="169rpx" labelAlign="center">
				<u--input v-model="user.name" border="none" placeholder="请输入收货人"></u--input>
			</u-form-item>
			<u-form-item label="手机号码" borderBottom labelWidth="169rpx" labelAlign="center">
				<u--input v-model="user.phone" border="none" placeholder="请输入手机号"></u--input>
			</u-form-item>
			<u-form-item label="所在地址" borderBottom labelWidth="169rpx" labelAlign="center">
				<u--input v-model="user.adress" border="none" placeholder="请输入所在地"></u--input>
			</u-form-item>
			<u-form-item label="详细地址" borderBottom labelWidth="169rpx" labelAlign="center">
				<u--textarea v-model="user.alladress" border="none" height="160rpx" placeholder="请输入内容">
				</u--textarea>
			</u-form-item>
			<u-form-item label="标签" borderBottom labelWidth="169rpx" labelAlign="center">
			<u-radio-group v-model="user.radiovalue1" placement="row">
					<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, index) in radiolist1" :key="index"
						:label="item.name" :name="item.name" @change="radioChange">
					</u-radio>
				</u-radio-group>


				<view class="set">
					设置默认地址
					<u-switch v-model="user.isshow" @change="change"></u-switch>
				</view>
			</u-form-item>
		</u--form>

		<view class="btn">
			<button @click="submit">保存并使用</button>
			<button @click="del">删除</button>
		</view>


	</view>
</template>

<script>
	import {
		addressadd,
		addressedit,
		Addressremove
	} from '@/request/app.js'
	export default {
		data() {
			return {
				radiolist1: [{
						name: '家',
						disabled: false
					},
					{
						name: '公司',
						disabled: false
					},
					{
						name: '学校',
						disabled: false
					}
				],
				user: {
					name: '',
					phone: '',
					adress: '',
					alladress: '',
					isshow: false,
					radiovalue1: '家',
				},
				info:{}
			
			}
		},
		methods: {
			// 删除按钮
			del(){
				Addressremove({
					id:this.user.id
				})
				.then(res=>{
					console.log(res,"删除成功");
					uni.navigateTo({
						url:'../order/order'
					})
				})
			},
			change(e) {
				console.log('change', e);
			},
			submit(e) {
				if (this.user.name == '') {
					uni.showToast({
						title: '请输入收件人'
					})
					return
				}
				if (this.user.phone == '') {
					uni.showToast({
						title: '请输入手机号'
					})
					return
				}

				if (this.user.adress == '') {
					uni.showToast({
						title: '请输入地址'
					})
					return
				}
				if (this.user.alladress == '') {
					uni.showToast({
						title: '请输入详细地址'
					})
					return
				}
				if(this.user.id){
					console.log(this.user,"编辑");
					addressedit({
						uid:uni.getStorageSync("userinfo").uid,
						id:this.user.id,
						location:this.user.adress,
						username:this.user.name,
						userphone:this.user.phone,
						useraddress:this.user.alladress,	
					})
					.then(res=>{
						console.log(res,"地址编辑")
						if(res.data.code==200){
							uni.navigateTo({
								url: '../order/order'
							})
						}
						
					})	
				}else{
					addressadd({
						uid: uni.getStorageSync("userinfo").uid,
						username: this.user.name,
						userphone: this.user.phone,
						location: this.user.adress,
						useraddress: this.user.alladress,
						status: this.user.isshow ? 1 : 2,
						label: this.user.radiovalue1
					}).then(res => {
						console.log(res);
						if (res.data.code == 200) {
							uni.navigateTo({
								url: '../order/order'
							})
						}
					})
				}
				
			},
			radioChange(e) {
				console.log(e);
				this.user.radiovalue1 = e
			}

		},
		onLoad(option) {
			console.log(option,"回");
			this.user=option
			console.log(this.user,"回显");
			// console.log(option,"回显");
			if(option.id){
				this.user.name=option.username 
				this.user.phone=option.userphone 
				this.user.adress=option.location 
				this.user.alladress=option.useraddress 
			}
			// this.info.username=this.user.name
			// this.info.userphone=this.user.phone
			// this.info.location=this.user.adress
			// this.info.useraddress=this.user.alladress
			// this.user=this.user.map(item=>{
			// 	return {
			// 		name:item.username,
			// 		phone:item.userphone,
			// 		adress:item.location,
			// 		alladress:item.useraddress
			// 	}
			// })
				
					
			
		}
	
	}
</script>

<style>
	.u-textarea.data-v-09988a29 {

		background-color: #eee !important;
		padding: 9px;
		color: #333;
	}

	.u-radio-group--row.data-v-97ce24d6 {
		display: flex;
		flex-direction: row;
		justify-content: space-evenly;
	}

	.set {
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
	}

	.btn {
		margin-top: 60rpx;
	}

	button {
		margin-bottom: 20rpx;
	}

	button:nth-child(1) {
		background-color: #FF6040;
		color: #fff;
		font-weight: bold;
		width: 90%;
	}

	button:nth-child(2) {
		font-weight: bold;
		width: 90%;
	}
</style>
